package com.chuangshi.core.vote.admin.controller.admin;

import com.chuangshi.core.exception.BusinessException;
import com.chuangshi.core.kit.Controller;
import com.chuangshi.core.kit.IdWorker;
import com.chuangshi.core.kit.Result;
import com.chuangshi.core.token.TokenService;
import com.chuangshi.core.vote.admin.entity.Admin;
import com.chuangshi.core.vote.admin.controller.admin.request.*;
import com.chuangshi.core.vote.admin.controller.admin.response.*;
import com.chuangshi.core.vote.admin.entity.column.AdminColumn;
import com.chuangshi.core.vote.admin.service.AdminService;

import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 管理员信息管理端控制器
 *
 * @author ggliming
 * @date 2019-02-21
 */
@Api(tags = "管理员信息管理端接口管理")
@RestController
public class AdminAdminController extends Controller {

    @Autowired
    private AdminService adminService;

    @Autowired
    private TokenService tokenService;

    @ApiOperationSort(value = 10)
    @ApiOperation(value = "获取管理员信息列表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "body", value = "请求参数", required = true, paramType = "body", dataType = "AdminAdminV1ListRequest")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "返回参数", response = AdminAdminV1ListResponse.class)
    })
    @RequestMapping(value = "/vote/admin/admin/v1/list", method = {RequestMethod.POST}, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> v1List(@ApiIgnore @RequestBody AdminAdminV1ListRequest body) {
        validateRequest(body);

        Result result = adminService.listForAdmin(body);

        validateResponse(AdminAdminV1ListResponse.class);

        return renderJson(result);
    }

    @ApiOperationSort(value = 20)
    @ApiOperation(value = "管理员登陆", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "body", value = "请求参数", required = true, paramType = "body", dataType = "AdminAdminV1LoginRequest")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "返回参数", response = AdminAdminV1LoginResponse.class)
    })
    @RequestMapping(value = "/vote/admin/admin/v1/login", method = {RequestMethod.POST}, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> v1Login(@ApiIgnore @RequestBody AdminAdminV1LoginRequest body) {
        validateRequest(body);

        Admin admin = adminService.findByAccount(body.getAdminAccount());
        if (admin == null || !admin.getAdminPassword().equals(body.getAdminPassword())) {
            throw new BusinessException("账号或密码不正确");
        }
        String token = tokenService.encode(admin.getAdminUserId(), admin.getAdminId(), admin.getAppId());
        AdminAdminV1LoginResponse response = new AdminAdminV1LoginResponse();
        response.setAdminAccount(admin.getAdminAccount());
        response.setToken(token);
        validateResponse(AdminAdminV1LoginResponse.class);
        return renderJson(response);
    }

    @ApiOperationSort(value = 20)
    @ApiOperation(value = "获取单条管理员信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "body", value = "请求参数", required = true, paramType = "body", dataType = "AdminAdminV1FindRequest")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "返回参数", response = AdminAdminV1FindResponse.class)
    })
    @RequestMapping(value = "/vote/admin/admin/v1/find", method = {RequestMethod.POST}, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> v1Find(@ApiIgnore @RequestBody AdminAdminV1FindRequest body) {
        validateRequest(body);

        Admin result = adminService.find(body.getAdminId());

        validateResponse(AdminAdminV1FindResponse.class);

        return renderJson(result);
    }

    @ApiOperationSort(value = 30)
    @ApiOperation(value = "新增管理员信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "body", value = "请求参数", required = true, paramType = "body", dataType = "AdminAdminV1SaveRequest")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "新增成功")
    })
    @RequestMapping(value = "/vote/admin/admin/v1/save", method = {RequestMethod.POST}, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> v1Save(@ApiIgnore @RequestBody AdminAdminV1SaveRequest body) {
        validateRequest(body);

        Admin admin = new Admin(body);

        String adminId = IdWorker.getIdStr();
        admin.setAdminId(adminId);
        admin.setAdminUserId(IdWorker.getIdStr());

        Boolean result = adminService.save(admin);

        return renderJson(result, result ? "新增成功" : "新增不成功");
    }

    @ApiOperationSort(value = 40)
    @ApiOperation(value = "修改管理员信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "body", value = "请求参数", required = true, paramType = "body", dataType = "AdminAdminV1UpdateRequest")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "修改成功")
    })
    @RequestMapping(value = "/vote/admin/admin/v1/update", method = {RequestMethod.POST}, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> v1Update(@ApiIgnore @RequestBody AdminAdminV1UpdateRequest body) {
        validateRequest(body);

        Admin admin = new Admin(body);

        Boolean result = adminService.update(admin, body.getSystemVersion());

        return renderJson(result, result ? "修改成功" : "修改不成功");
    }

    @ApiOperationSort(value = 50)
    @ApiOperation(value = "删除管理员信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "body", value = "请求参数", required = true, paramType = "body", dataType = "AdminAdminV1DeleteRequest")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "删除成功")
    })
    @RequestMapping(value = "/vote/admin/admin/v1/delete", method = {RequestMethod.POST}, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> v1Delete(@ApiIgnore @RequestBody AdminAdminV1DeleteRequest body) {
        validateRequest(body);

        Boolean result = adminService.delete(body.getAdminIdList());

        return renderJson(result, result ? "删除成功" : "删除不成功");
    }

}